package com.imooc.order.service.impl;

import com.imooc.order.client.ProductClient;
import com.imooc.order.dataobject.OrderDetail;
import com.imooc.order.dataobject.ProductInfo;
import com.imooc.order.dto.CartDTO;
import com.imooc.order.dto.OrderDTO;
import com.imooc.order.dao.OrderDetailDao;
import com.imooc.order.dao.OrderMasterDao;
import com.imooc.order.dataobject.OrderMaster;
import com.imooc.order.service.OrderService;
import com.imooc.order.utils.Constant;
import com.imooc.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDetailDao orderDetailDao;

    @Autowired
    private OrderMasterDao orderMasterDao;

    @Autowired
    private ProductClient productClient;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {

        String orderId = KeyUtil.getUniqueKey();
        //  查询商品信息 (调用商品服务)
        List<String> productIdList = orderDTO.getOrderDetailList().stream().map(OrderDetail::getProductId).collect(Collectors.toList());
        List<ProductInfo> productInfos = productClient.listForOrder(productIdList);

        BigDecimal amount = new BigDecimal(0);
        // 计算总价
        for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
            //单价*数量
            for (ProductInfo productInfo : productInfos) {
                if (orderDetail.getProductId().equals(productInfo.getProductId())) {
                    amount = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity())).add(amount);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.getUniqueKey());
                    orderDetail.setProductIcon(productInfo.getProductIcon());
                    orderDetail.setProductPrice(productInfo.getProductPrice());
                    orderDetail.setProductName(productInfo.getProductName());
                    orderDetail.setCreateTime(new Date());
                    orderDetail.setUpdateTime(new Date());
                    //订单详情入库
                    orderDetailDao.save(orderDetail);
                }
            }

        }

        // 扣库存  (调用商品服务)
        List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream().
                map(e -> new CartDTO(e.getProductId(), e.getProductQuantity())).collect(Collectors.toList());
        productClient.decreaseStock(cartDTOList);


        //订单入库
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderStatus(Constant.OrderStatus.NEW);
        orderMaster.setPayStatus(Constant.PayStatus.WAIT);

        orderMaster.setOrderAmount(amount);
        orderMaster.setCreateTime(new Date());
        orderMaster.setUpdateTime(new Date());
        orderMasterDao.save(orderMaster);

        return orderDTO;
    }
}


